/*
 * new-item.c
 *
 *  Created on: 21-dic-2008
 *      Author: igor
 */

#include "view.h"

const char* duedate;	// due date for a task

/*
 * print a date in the format specified by format
 * http://publications.gbdirect.co.uk/c_book/chapter9/date_and_time.html
 * http://cc.byexamples.com/20070126/create-your-own-time-stamp/
%a  	abbreviated weekday name
%A 	full weekday name
%b 	abbreviated month name
%B 	full month name
%c 	date and time representation
%d 	decimal day of month number 01–31
%H 	hour 00–23 (24 hour format)
%I 	hour 01–12 (12 hour format)
%j 	day of year 001–366
%m 	month 01–12
%M 	minute 00–59
%p 	local equivalent of ‘AM’ or ‘PM’
%S 	second 00–61
%U 	week number in year 00–53 (Sunday is first day of week
%w 	weekday, 0–6 (Sunday is 0)
%W 	week number in year 00–53 (Monday is first day of week
%x 	local date representation
%X 	local time representation
%y 	year without century prefix 00–99
%Y 	year with century prefix
%Z 	timezone name, or no characters if no timezone exists
%% 	a % character
 *
 */
char
*timestamp(int lt, const char *format)
{
	static char buf[PATH_MAX];
	size_t i;
	struct tm tim;
	time_t now;

	now = time(NULL);

	// add an arbitrary diff in time from now
	if (lt != 0)
		now = now + lt;

	tim = *(localtime(&now));
	if (format != NULL) {
		i = strftime(buf,PATH_MAX,format,&tim);
	} else {
		i = strftime(buf,PATH_MAX,"%a %b %d %H:%M:%S %Y\n",&tim);
	}

	if (i > 0)
		return (char *)buf;
	else
		return (char *)NULL;

}

/*
 * TODO -
 * set categories to new task
 */
void
my_entry_bt_add_categories(void *data, Evas_Object *obj, void *event_info)
{

   printf ("DEBUG: <my_entry_bt_add_categories>\n");

	int i = 0;

	if (tmp != NULL) {
		printf("\nDEBUG: categories %d\n", sizeof(*tmp));
		for (i = 0; i < sizeof(*tmp)+1; i++) {
			printf("DEBUG: %d %s\n", i, tmp[i]);
		}
	} else {
		printf ("DEBUG: data = %s", tmp);
	}

	Evas_Object *win, *bg, *li, *ic, *ic2, *bx, *ly;
	char buf[PATH_MAX];

	win = elm_win_add(NULL, "list-2", ELM_WIN_BASIC);
	elm_win_title_set(win, "List 2");
	elm_win_autodel_set(win, 1);

	bg = elm_bg_add(win);
	elm_win_resize_object_add(win, bg);
	evas_object_size_hint_weight_set(bg, 1.0, 1.0);
	evas_object_show(bg);

	ly = elm_layout_add(win);
	snprintf(buf, sizeof(buf), "%s/%s.edj", PIXMAPS_DIR, PACKAGE);
	printf ("DEBUG layout: %s;\n", buf);
	elm_layout_file_set(ly, buf, "change_cat");
	elm_win_resize_object_add(win, ly);
	evas_object_show(ly);

	li = elm_list_add(win);
	elm_win_resize_object_add(win, li);
	evas_object_size_hint_weight_set(li, 1.0, 1.0);
	elm_list_horizontal_mode_set(li, ELM_LIST_LIMIT);
	elm_list_multi_select_set(li, 1);

	ic = elm_icon_add(win);
	snprintf(buf, sizeof(buf), "/usr/share/elementary/images/logo_small.png");
	elm_icon_file_set(ic, buf, NULL);
	elm_list_item_append(li, "Hello", ic, NULL,  NULL, NULL);
	ic = elm_icon_add(win);
	snprintf(buf, sizeof(buf), "/usr/share/elementary/images/logo_small.png");
	elm_icon_scale_set(ic, 0, 0);
	elm_icon_file_set(ic, buf, NULL);
	elm_list_item_append(li, "world", ic, NULL,  NULL, NULL);
	ic = elm_icon_add(win);
	elm_icon_standard_set(ic, "edit");
	elm_icon_scale_set(ic, 0, 0);
	elm_list_item_append(li, ".", ic, NULL,  NULL, NULL);

	ic = elm_icon_add(win);
	elm_icon_standard_set(ic, "delete");
	elm_icon_scale_set(ic, 0, 0);
	ic2 = elm_icon_add(win);
	elm_icon_standard_set(ic2, "clock");
	elm_icon_scale_set(ic2, 0, 0);
	elm_list_item_append(li, "How", ic, ic2,  NULL, NULL);

	bx = elm_box_add(win);
	elm_box_horizontal_set(bx, 1);

	ic = elm_icon_add(win);
	snprintf(buf, sizeof(buf), "/usr/share/elementary/images/logo_small.png");
	elm_icon_file_set(ic, buf, NULL);
	elm_icon_scale_set(ic, 0, 0);
	evas_object_size_hint_align_set(ic, 0.5, 0.5);
	elm_box_pack_end(bx, ic);
	evas_object_show(ic);

	ic = elm_icon_add(win);
	snprintf(buf, sizeof(buf), "/usr/share/elementary/images/logo_small.png");
	elm_icon_file_set(ic, buf, NULL);
	elm_icon_scale_set(ic, 0, 0);
	evas_object_size_hint_align_set(ic, 0.5, 0.0);
	elm_box_pack_end(bx, ic);
	evas_object_show(ic);

	struct charlist *catlist = NULL;
	catlist = (struct charlist *) leggi_cat_da_db(catlist);

	if (catlist == NULL)
	{
		printf("DEBUG: lista vuota;\n");
	} else {

		int i = 0;
		while (catlist != NULL)
		{
			elm_list_item_append(li, catlist->inf, bx, NULL,  NULL, NULL);

			// elemento successivo
			catlist = catlist->pun;

		}
	}

	elm_list_item_append(li, "are", bx, NULL,  NULL, NULL);

	elm_list_go(li);
	evas_object_show(li);

	// TODO - visualizzare i bottoni aggiustando il tema
	Evas_Object *bt_new, *bt_quit;
	bt_new = elm_button_add(win);
	window_swallow(ly, "button_add", bt_new);
	elm_button_label_set(bt_new, "New");
	evas_object_smart_callback_add(bt_new, "clicked", my_win_close, win);
//	evas_object_smart_callback_add(bt_new, "clicked", my_win_new_cat, mother);
	evas_object_size_hint_align_set(bt_new, 1.0, 1.0);
	evas_object_show(bt_new);

	/* create a button */
	bt_quit = elm_button_add(win);
	window_swallow(ly, "button_quit", bt_quit);
	/* set the button label */
	elm_button_label_set(bt_quit, "Quit");
	/* add a callback to be run when the button is clicked */
	evas_object_smart_callback_add(bt_quit, "clicked", my_win_close, win);
//	evas_object_smart_callback_add(bt_quit, "clicked", my_entry_bt_refresh, mother);
	/* the button should fill any space it is given horizontally */
	evas_object_size_hint_align_set(bt_quit, 1.0, 1.0);
	/* show the button */
	evas_object_show(bt_quit);


	evas_object_resize(win, 320, 300);
	evas_object_show(win);
   printf ("DEBUG: </my_entry_bt_add_categories>\n");

}

/* set a due date for a task
 * data is the due date to set */
void
my_entry_bt_duedate(void *data, Evas_Object *obj, void *event_info)
{
   printf ("DEBUG: <my_entry_bt_duedate>\n");
   // set duedate to the value of data
   printf("DEBUG: duedate (data) %s\n", (char *)data);

   char *buf = (char *)data;
   duedate = (char *)malloc(PATH_MAX);

   sprintf((char *)duedate, (const char *)buf);

   printf("DEBUG: duedate %s\n", (char *)duedate);
   printf ("DEBUG: </my_entry_bt_duedate>\n");

}

/* add a new note in storage,
 * data is a etodoitem representing note to add */
void
my_entry_bt_add_item(void *data, Evas_Object *obj, void *event_info)
{

   printf ("DEBUG: <my_entry_bt_add_item>\n");
   /* get param data */
   struct etodoitem *etdi = data;
   /* data to store in the db */
   struct todoitem *tdi = (struct todoitem *)malloc(sizeof(struct todoitem));
   char *title = (char *)elm_entry_entry_get(etdi->inf.title);
   /* allocate memory */
   const char *icon = calloc (1, 2048);
   const char *infos = calloc (1, 2048);
   char *text = (char *)elm_entry_entry_get(etdi->inf.text);

   // Remove trailing "<br>"
   if(strlen(title) >= 4 && !strncmp(title + (strlen(title)-4), "<br>", 4)) {
       title[strlen(title) - 4] = '\0';
   }

   sprintf(tdi->inf.title, title);
   sprintf(tdi->inf.text, text);
   // TODO - aggiungere la data al db
   sprintf(tdi->inf.duedate, duedate);

   printf("DEBUG: title: %s\n", title);

   if (etdi->inf.infos == NULL)
   {
	  infos = (const char *) get_time_str(0);
	  // TODO - controllare come mai non funziona snprintf
//      snprintf(tdi->inf.infos, strlen(infos), infos);
      sprintf(tdi->inf.infos, infos);
      printf("DEBUG: infos: %s\n", tdi->inf.infos);
   }
   else
   {
	  printf("DEBUG infos (etdi): %s\n", (char *)etdi->inf.infos);
   }

   if (etdi->inf.icon == NULL)
   {
       icon = "logo_small";
       sprintf(tdi->inf.icon, icon);
       printf("DEBUG icon: %s\n", tdi->inf.icon);
   }
   else
   {
//      printf("icon: %s\n", elm_entry_entry_get(etdi->inf.icon));
   }

//   printf("text: %s\n", elm_entry_entry_get(tdi->inf.text));

   add_item_into_db(tdi); // restituisce NULL se qualcosa non va per il verso giusto

   // Esce dalla funzione
   return;

}

/* prepara i dati per una nuova nota
 *
 * setup data for a new item: show a win with
 * a text box for the title and one for item
 * content, then a button to save note and
 * close the win
 */
void
my_entry_bt_new_item(void *data, Evas_Object *obj, void *event_info)
{
   printf("DEBUG: <my_entry_bt_new_item>\n");

   Evas_Object *win, *bg, *bx, *bx2, *bt, *tit, *txt, *en, *sc, *sc_tit, *ly;
   char buf[PATH_MAX];
   char *categories[PATH_MAX]; *categories = NULL;
   Evas_Object *mother = data;
   struct etodoitem *p = NULL;

   win = elm_win_add(NULL, PACKAGE " item entry", ELM_WIN_BASIC);
   elm_win_title_set(win, PACKAGE " - item entry");
   elm_win_autodel_set(win, 1);

   bg = elm_bg_add(win);
   elm_win_resize_object_add(win, bg);
   evas_object_size_hint_weight_set(bg, 1.0, 1.0);
   evas_object_show(bg);

   ly = elm_layout_add(win);
   snprintf(buf, sizeof(buf), "%s/%s.edj", PIXMAPS_DIR, PACKAGE);
   printf ("DEBUG layout: %s;\n", buf);
   elm_layout_file_set(ly, buf, "add_item");
   elm_win_resize_object_add(win, ly);
   evas_object_show(ly);

   bx = elm_box_add(win);
   window_swallow(ly, "contenuto_bx", bx);
   evas_object_size_hint_weight_set(bx, 1.0, 1.0);
   evas_object_resize(bx, 320, 300);
   evas_object_show(bx);

   tit = elm_entry_add(win);
   window_swallow(ly, "title_entry", tit);
   elm_entry_line_wrap_set(tit, 0);
   elm_entry_entry_set(tit, "");
   evas_object_size_hint_weight_set(tit, 1.0, 0.0);
   evas_object_size_hint_align_set(tit, -1.0, -1.0);
   elm_entry_select_all(tit);
   elm_box_pack_end(bx, tit);
   evas_object_show(tit);

   sc_tit = elm_scroller_add(win);
   window_swallow(ly, "title_entry_sc", sc_tit);
   elm_scroller_content_min_limit(sc_tit, 0, 1);
   evas_object_size_hint_weight_set(sc_tit, 1.0, 0.0);
//   evas_object_size_hint_align_set(sc_tit, -1.0, -1.0);
   elm_box_pack_end(bx, sc_tit);
   evas_object_show(sc_tit);

   sc = elm_scroller_add(win);
   window_swallow(ly, "text_entry_sc", sc);
   evas_object_size_hint_weight_set(sc, 1.0, 1.0);
   evas_object_size_hint_align_set(sc, -1.0, -1.0);
   elm_box_pack_end(bx, sc);
   evas_object_show(sc);

   txt = elm_entry_add(win);
   window_swallow(ly, "text_entry", txt);
   elm_entry_entry_set(txt, "");
   evas_object_size_hint_weight_set(txt, 1.0, 1.0);
   evas_object_size_hint_align_set(txt, -1.0, 0.0);
   elm_scroller_content_set(sc, txt);
   evas_object_show(txt);

   /* add a box layout widget to the window */
   bx2 = elm_box_add(win);
   window_swallow(ly, "footer_bx", bx2);
   elm_box_horizontal_set(bx2, 1);
   evas_object_size_hint_weight_set(bx2, 1.0, 1.0);
   evas_object_size_hint_align_set(bx2, -1.0, -1.0);
   evas_object_show(bx2);

   // TODO - add multiple categories
   bt = elm_button_add(win);
   window_swallow(ly, "button_categories", bt);
   elm_button_label_set(bt, "Add categories");
   // se esegue prima la chiusura poi non modifica
   evas_object_smart_callback_add(bt, "clicked", my_entry_bt_add_categories, categories);
   evas_object_size_hint_weight_set(bt, 1.0, 1.0);
   elm_box_pack_end(bx2, bt);
   evas_object_show(bt);

   // When a user clicks on the due date, a list of the next four days
   // is presented along with an option to remove the date or select a
   // date from a calendar.
   bt = elm_hoversel_add(win);
   window_swallow(ly, "button_date", bt);
   elm_hoversel_hover_parent_set(bt, win);
   elm_hoversel_label_set(bt, "Due Date");
// elm_hoversel_item_add(Evas_Object *obj, const char *label, const char *icon_file,
//   Elm_Icon_Type icon_type, void (*func) (void *data, Evas_Object *obj, void *event_info),
//   const void *data)
   char *zero, *one, *two, *three, *four;

   sprintf(buf,"%s - Today", timestamp(0, "%d/%m"));
   if (buf != NULL) {
//	   printf ("DEBUG: time: %s\n", buf);
	   zero = (char *)malloc(PATH_MAX);
	   sprintf(zero, timestamp(0, NULL));
	   zero[strlen(zero) -1] = '\0';
	   printf ("DEBUG: time (zero): %s\n", zero);
	   elm_hoversel_item_add(bt, buf, NULL, ELM_ICON_NONE, my_entry_bt_duedate, (char *)zero);
   }

   sprintf(buf,"%s - Tomorrow", timestamp(86400, "%d/%m"));
   if (buf != NULL) {
	   one = (char *)malloc(PATH_MAX);
	   sprintf(one, timestamp(86400, NULL));
	   one[strlen(one) -1] = '\0';
	   printf ("DEBUG: time (one): %s\n", one);
	   elm_hoversel_item_add(bt, buf, NULL, ELM_ICON_NONE, my_entry_bt_duedate, (char *)one);
   }

   sprintf(buf, timestamp(86400*2, "%d/%m - %a"));
   if (buf != NULL) {
	   two = (char *)malloc(PATH_MAX);
	   sprintf(two, timestamp(86400*2, NULL));
	   two[strlen(two) -1] = '\0';
	   printf ("DEBUG: time (two): %s\n", two);
	   elm_hoversel_item_add(bt, buf, NULL, ELM_ICON_NONE, my_entry_bt_duedate, (char *)two);
   }

   sprintf(buf, timestamp(86400*3, "%d/%m - %a"));
   if (buf != NULL) {
	   three  = (char *)malloc(PATH_MAX);
	   sprintf(three, timestamp(86400*3, NULL));
	   three[strlen(three) -1] = '\0';
	   printf ("DEBUG: time (three): %s\n", three);
	   elm_hoversel_item_add(bt, buf, NULL, ELM_ICON_NONE, my_entry_bt_duedate, (char *)three);
   }

   sprintf(buf, timestamp(86400*4, "%d/%m - %a"));
   if (buf != NULL) {
       four = (char *)malloc(PATH_MAX);
   	   sprintf(four, timestamp(86400*4, NULL));
   	   four[strlen(four) -1] = '\0';
   	   printf ("DEBUG: time (four): %s\n", four);
   	   elm_hoversel_item_add(bt, buf, NULL, ELM_ICON_NONE, my_entry_bt_duedate, (char *)four);
   }

   elm_hoversel_item_add(bt, "No Date", NULL, ELM_ICON_NONE, my_entry_bt_duedate, NULL);

   // TODO - visualizzare un calendario
   elm_hoversel_item_add(bt, "Choose Date ..", NULL, ELM_ICON_NONE, NULL, NULL);

   evas_object_size_hint_weight_set(bt, 0.0, 0.0);
   evas_object_size_hint_align_set(bt, 0.5, 0.5);
   elm_box_pack_end(bx2, bt);
   evas_object_show(bt);

   e_todoitem daInserire;
   p = (struct etodoitem *)malloc(sizeof(struct etodoitem));
   daInserire.title = tit;
   daInserire.infos = NULL;
   daInserire.duedate = (char *)duedate;
   daInserire.icon = NULL;
   daInserire.text = txt;
   p->inf = daInserire;
   p->pun = NULL;

   bt = elm_button_add(win);
   window_swallow(ly, "button_save", bt);
   elm_button_label_set(bt, "Save");
   // se esegue prima la chiusura poi non modifica
   evas_object_smart_callback_add(bt, "clicked", my_win_close, win);
   evas_object_smart_callback_add(bt, "clicked", my_entry_bt_refresh, mother);
   evas_object_smart_callback_add(bt, "clicked", my_entry_bt_add_item, p);
   evas_object_size_hint_weight_set(bt, 1.0, 1.0);
   elm_box_pack_end(bx2, bt);
   evas_object_show(bt);

   evas_object_resize(win, 320, 300);

   // FIXME: not exported
   elm_widget_focus_set(win, 1);
   evas_object_show(win);

   printf("DEBUG: </my_entry_bt_new_item>\n");

}
